package com.mabang.medata.client;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.log4j.Logger;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mabang.medata.client.authc.ClientPasswordCallback;
import com.mabang.medata.procotol.QuoteProcotol;
import com.mabang.medata.service.HelloService;
import com.mabang.medata.service.QuoteService;

public class Client {
	static Logger logger = Logger.getLogger(Client.class);

	public static void main(String[] args) throws Exception {
//		Client.dynamic();
//		Client.hello();
//		Client.helloJAX();
		Client.JaxWsDynamic();
//		Client.spring();
	}

	/**
	 * 
	* @Title: jaxWsClient
	* @Description: 静态代理客户端
	* @return void
	* @throws
	 */
	public static void jaxWsClient() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://localhost:8080/ws/getquoteno");
		factory.setServiceClass(QuoteService.class);
		QuoteService helloService = factory.create(QuoteService.class);
		String quote = QuoteProcotol.quoteNo("B-001001-201504070199");
		System.out.println(helloService.getQuoteNo(quote));
	}

	/**
	 * 
	* @Title: spring
	* @Description: spring 客户端调用
	* @return void
	* @throws
	 */
	public static void spring() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:spring-client.xml");
		QuoteService quoteService = context.getBean("quoteService",
				QuoteService.class);
		String quote = QuoteProcotol.quoteNo("B-001001-201504070199");
		String result = quoteService.getQuoteNo(quote);
		System.out.println(result);
	}
	
	/**
	 * 
	* @Title: JaxWsDynamic
	* @Description:动态代理客户端
	* @return void
	* @throws
	 */
	public static void JaxWsDynamic(){
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = factory.createClient("http://localhost:8080/ws/getquoteno?wsdl");
        try {
            Object[] results = client.invoke("getquoteno", QuoteProcotol.quoteNo("B-001001-201504070199"));
            System.out.println(results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 
	* @Title: dynamic
	* @Description: 通用代理客户端
	* @return void
	* @throws
	 */
	public static void dynamic(){
		DynamicClientFactory factory = DynamicClientFactory.newInstance();
		org.apache.cxf.endpoint.Client client = factory.createClient("http://localhost:8080/ws/getquoteno?wsdl");
        try {
        	 Object[] results = client.invoke("getquoteno", QuoteProcotol.quoteNo("B-001001-201504070199"));
             System.out.println(results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	public static void hello(){
//		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//		factory.setAddress("http://localhost:8080/ws/hello");
//		factory.setServiceClass(QuoteService.class);
//		HelloService helloService = factory.create(HelloService.class);
//		System.out.println(helloService.say("yuyidi"));
		
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:spring-client.xml");
		HelloService helloService = context.getBean("helloService",
				HelloService.class);
		String result = helloService.say("余乙迪");
		System.out.println(result);
	}
	
	public static void helloJAX(){
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = factory.createClient("http://localhost:8080/ws/hello?wsdl");
        Endpoint point = client.getEndpoint();  
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        map.put(WSHandlerConstants.USER, "client");
        map.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
        map.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordCallback.class.getName());
        point.getOutInterceptors().add(new WSS4JOutInterceptor(map));
        try {
			Object[] results = client.invoke("say", "yuyidi");
			System.out.println(results[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

